package com.ld.security.auth.common.handler;

import com.ld.security.auth.common.impl.ApiLogQueueImpl;
import com.ld.security.auth.common.po.ApiLogPo;
import com.ld.security.common.core.base.vo.DataVo;
import com.ld.security.common.core.base.vo.ExceptionVo;
import com.ld.security.common.core.exception.ApiException;
import com.ld.security.common.core.exception.SuperException;
import com.ld.security.common.tomcat.handler.BaseExceptionHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 异常日志记录
 */
@ControllerAdvice
public class AuthExceptionHandler extends BaseExceptionHandler {


@Autowired(required = false)
	ApiLogQueueImpl apiLogQueue;

	 void sendMsg(DataVo<ExceptionVo> vo){
	 	if(apiLogQueue == null){
	 		return;
		}
		ExceptionVo ex =  vo.getData();
		ApiLogPo log = new ApiLogPo();
		log.setContent(ex.getContent());
		log.setLabel(vo.getMessage());
		log.setContent(ex.getContent());
		log.setCode(vo.getCode());
		apiLogQueue.push(log);
	}

	@Override
	public DataVo<ExceptionVo> ApiException(ApiException ex, HttpServletRequest req, HttpServletResponse resp) {
		DataVo<ExceptionVo>  vo= super.Exception(ex, req, resp);
		this.sendMsg(vo);
		return  vo;
	}

	@ResponseBody
	@Override
	public DataVo<ExceptionVo> Exception(Exception ex, HttpServletRequest req, HttpServletResponse resp) {
		DataVo<ExceptionVo>  vo= super.Exception(ex, req, resp);
		this.sendMsg(vo);
		return  vo;
	}

}
